<?php

namespace App\Http\Controllers\Backend;

use App;
use DB;
use Auth;
use App\Criteria\Base\DataItemDetail\DataItemCriteria;
use Illuminate\Support\Facades\View;
use App\Services\Auth\ModuleService;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use App\Repositories\Base\DataItemDetailRepository;

class HomeController extends Controller
{
    use AuthenticatesUsers;
    protected $menuService;

    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     */
    public function __construct(ModuleService $menuService)
    {
        $this->menuService = $menuService;
    }

    protected function guard()
    {
        return Auth::guard('manager');
    }

    public function username()
    {
        return 'username';
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $manager = Auth::guard('manager')->user();
        $menus = $this->menuService->getManagerModelsHtml($manager);

        return View::make('backend.home', compact('menus'));
    }

    public function getLogin()
    {
        return View::make('backend.login');
    }

    public function welcome()
    {
        return View::make('backend.welcome');
    }

    public function updateSchoolData()
    {
        DB::beginTransaction();
        try {
            // /updateSchoolData?table=ft_lesson_signup&setColumn=school_id&whereColumn=school
            $requestData = request()->all();
            $table = isset($requestData['table']) ? $requestData['table'] : 'ft_user';
            $setColumn = isset($requestData['setColumn']) ? $requestData['setColumn'] : 'school_id';
            $whereColumn = isset($requestData['whereColumn']) ? $requestData['whereColumn'] : 'school';
            $data = DB::select('select * from schools where school_id != ""');
            foreach ($data as $value) {
                DB::update('update ' . $table . ' set ' . $setColumn . ' = ? where ' . $whereColumn . ' = ?', [$value->school_id, $value->old_school]);
            }
            DB::commit();

            return $data;
        } catch (Exception $ex) {
            DB::rollBack();

            return $ex->getMessage();
        }
    }

    public function updateMajorData()
    {
        DB::beginTransaction();
        try {
            $requestData = request()->all();
            $table = isset($requestData['table']) ? $requestData['table'] : 'ft_user';
            $setColumn = isset($requestData['setColumn']) ? $requestData['setColumn'] : 'major_id';
            $whereColumn = isset($requestData['whereColumn']) ? $requestData['whereColumn'] : 'major';
            $data = DB::select('select * from majors where major_id != ""');
            foreach ($data as $value) {
                DB::update('update ' . $table . ' set ' . $setColumn . ' = ? where ' . $whereColumn . ' = ?', [$value->major_id, $value->old_major]);
            }
            DB::commit();

            return $data;
        } catch (Exception $ex) {
            DB::rollBack();

            return $ex->getMessage();
        }
    }

    public function updateGradeData()
    {
        DB::beginTransaction();
        try {
            $requestData = request()->all();
            $table = isset($requestData['table']) ? $requestData['table'] : 'ft_user';
            $setColumn = isset($requestData['setColumn']) ? $requestData['setColumn'] : 'grade_id';
            $whereColumn = isset($requestData['whereColumn']) ? $requestData['whereColumn'] : 'grade';
            App::make(DataItemDetailRepository::class)->pushCriteria(DataItemCriteria::class);
            $itemData = App::make(DataItemDetailRepository::class)->scopeQuery(function ($query) {
                return $query->where('item_id', 'f1b1f2ed-36ce-47ae-b508-b6e92e42376a');
            })->get(['item_detail_id', 'item_value']);
            $data = DB::select('select * from grades where new_grade != ""');
            foreach ($data as $value) {
                $k = $itemData->firstWhere('item_value', $value->new_grade);
                DB::update('update ' . $table . ' set ' . $setColumn . ' = ? where ' . $table . '.' . $whereColumn . ' = ?', [$k->item_detail_id, $value->old_grade]);
            }
            $data = DB::select('select * from grades where grade != "其他" and grade != "待定" and new_grade is null');
            foreach ($data as $value) {
                switch ($value->grade) {
                    case '大一':
                        foreach ($itemData as $item) {
                            DB::update('update ' . $table . ' set ' . $setColumn . ' = ? where month(' . $table . '.created_at) >= 9 and ' . $table . '.' . $whereColumn . ' = ? and year(' . $table . '.created_at) = ?', [$item->item_detail_id, $value->old_grade, $item->item_value]);
                            DB::update('update ' . $table . ' set ' . $setColumn . ' = ? where month(' . $table . '.created_at) < 9 and ' . $table . '.' . $whereColumn . ' = ? and (year(' . $table . '.created_at)-1) = ?', [$item->item_detail_id, $value->old_grade, $item->item_value]);
                        }
                        break;
                    case '研一':
                        foreach ($itemData as $item) {
                            DB::update('update ' . $table . ' set ' . $setColumn . ' = ? where month(' . $table . '.created_at) >= 9 and ' . $table . '.' . $whereColumn . ' = ? and year(' . $table . '.created_at) = ?', [$item->item_detail_id, $value->old_grade, $item->item_value]);
                            DB::update('update ' . $table . ' set ' . $setColumn . ' = ? where month(' . $table . '.created_at) < 9 and ' . $table . '.' . $whereColumn . ' = ? and (year(' . $table . '.created_at)-1) = ?', [$item->item_detail_id, $value->old_grade, $item->item_value]);
                        }
                        break;
                    case '硕一':
                        foreach ($itemData as $item) {
                            DB::update('update ' . $table . ' set ' . $setColumn . ' = ? where month(' . $table . '.created_at) >= 9 and ' . $table . '.' . $whereColumn . ' = ? and year(' . $table . '.created_at) = ?', [$item->item_detail_id, $value->old_grade, $item->item_value]);
                            DB::update('update ' . $table . ' set ' . $setColumn . ' = ? where month(' . $table . '.created_at) < 9 and ' . $table . '.' . $whereColumn . ' = ? and (year(' . $table . '.created_at)-1) = ?', [$item->item_detail_id, $value->old_grade, $item->item_value]);
                        }
                        break;
                    case '大二':
                        foreach ($itemData as $item) {
                            DB::update('update ' . $table . ' set ' . $setColumn . ' = ? where month(' . $table . '.created_at) >= 9 and ' . $table . '.' . $whereColumn . ' = ? and (year(' . $table . '.created_at)-1) = ?', [$item->item_detail_id, $value->old_grade, $item->item_value]);
                            DB::update('update ' . $table . ' set ' . $setColumn . ' = ? where month(' . $table . '.created_at) < 9 and ' . $table . '.' . $whereColumn . ' = ? and (year(' . $table . '.created_at)-2) = ?', [$item->item_detail_id, $value->old_grade, $item->item_value]);
                        }
                        break;
                    case '研二':
                        foreach ($itemData as $item) {
                            DB::update('update ' . $table . ' set ' . $setColumn . ' = ? where month(' . $table . '.created_at) >= 9 and ' . $table . '.' . $whereColumn . ' = ? and (year(' . $table . '.created_at)-1) = ?', [$item->item_detail_id, $value->old_grade, $item->item_value]);
                            DB::update('update ' . $table . ' set ' . $setColumn . ' = ? where month(' . $table . '.created_at) < 9 and ' . $table . '.' . $whereColumn . ' = ? and (year(' . $table . '.created_at)-2) = ?', [$item->item_detail_id, $value->old_grade, $item->item_value]);
                        }
                        break;
                    case '硕二':
                        foreach ($itemData as $item) {
                            DB::update('update ' . $table . ' set ' . $setColumn . ' = ? where month(' . $table . '.created_at) >= 9 and ' . $table . '.' . $whereColumn . ' = ? and (year(' . $table . '.created_at)-1) = ?', [$item->item_detail_id, $value->old_grade, $item->item_value]);
                            DB::update('update ' . $table . ' set ' . $setColumn . ' = ? where month(' . $table . '.created_at) < 9 and ' . $table . '.' . $whereColumn . ' = ? and (year(' . $table . '.created_at)-2) = ?', [$item->item_detail_id, $value->old_grade, $item->item_value]);
                        }
                        break;
                    case '大三':
                        foreach ($itemData as $item) {
                            DB::update('update ' . $table . ' set ' . $setColumn . ' = ? where month(' . $table . '.created_at) >= 9 and ' . $table . '.' . $whereColumn . ' = ? and (year(' . $table . '.created_at)-2) = ?', [$item->item_detail_id, $value->old_grade, $item->item_value]);
                            DB::update('update ' . $table . ' set ' . $setColumn . ' = ? where month(' . $table . '.created_at) < 9 and ' . $table . '.' . $whereColumn . ' = ? and (year(' . $table . '.created_at)-3) = ?', [$item->item_detail_id, $value->old_grade, $item->item_value]);
                        }
                        break;
                    case '研三':
                        foreach ($itemData as $item) {
                            DB::update('update ' . $table . ' set ' . $setColumn . ' = ? where month(' . $table . '.created_at) >= 9 and ' . $table . '.' . $whereColumn . ' = ? and (year(' . $table . '.created_at)-2) = ?', [$item->item_detail_id, $value->old_grade, $item->item_value]);
                            DB::update('update ' . $table . ' set ' . $setColumn . ' = ? where month(' . $table . '.created_at) < 9 and ' . $table . '.' . $whereColumn . ' = ? and (year(' . $table . '.created_at)-3) = ?', [$item->item_detail_id, $value->old_grade, $item->item_value]);
                        }
                        break;
                    case '硕三':
                        foreach ($itemData as $item) {
                            DB::update('update ' . $table . ' set ' . $setColumn . ' = ? where month(' . $table . '.created_at) >= 9 and ' . $table . '.' . $whereColumn . ' = ? and (year(' . $table . '.created_at)-2) = ?', [$item->item_detail_id, $value->old_grade, $item->item_value]);
                            DB::update('update ' . $table . ' set ' . $setColumn . ' = ? where month(' . $table . '.created_at) < 9 and ' . $table . '.' . $whereColumn . ' = ? and (year(' . $table . '.created_at)-3) = ?', [$item->item_detail_id, $value->old_grade, $item->item_value]);
                        }
                        break;
                    case '大四':
                        foreach ($itemData as $item) {
                            DB::update('update ' . $table . ' set ' . $setColumn . ' = ? where month(' . $table . '.created_at) >= 9 and ' . $table . '.' . $whereColumn . ' = ? and (year(' . $table . '.created_at)-3) = ?', [$item->item_detail_id, $value->old_grade, $item->item_value]);
                            DB::update('update ' . $table . ' set ' . $setColumn . ' = ? where month(' . $table . '.created_at) < 9 and ' . $table . '.' . $whereColumn . ' = ? and (year(' . $table . '.created_at)-4) = ?', [$item->item_detail_id, $value->old_grade, $item->item_value]);
                        }
                        break;
                    default:
                        break;
                }
            }
            DB::commit();

            return $data;
        } catch (Exception $ex) {
            DB::rollBack();

            return $ex->getMessage();
        }
    }
}
